package com.example.hm_dtfkz;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.base.BaseLibrary;
import com.example.hm_dtfkz.app.QuestionsApp;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.databinding.ActivityMainBinding;
import com.example.hm_dtfkz.ui.base.VBBaseActivity;
import com.example.hm_dtfkz.ui.fragment.BillRunFragment;
import com.example.hm_dtfkz.ui.fragment.HomeRunFragment;
import com.example.hm_dtfkz.ui.fragment.MeFragment;
import com.example.hm_dtfkz.BuildConfig;
import com.tencent.mm.opensdk.utils.ILog;

import java.util.Map;

@Route(path = "/test/activity")
public class MainActivity extends VBBaseActivity {

    private ActivityMainBinding mainBinding;
    private HomeRunFragment homeFragment;
    private BillRunFragment billRunFragment;
    private MeFragment meFragment;

    private final ActivityResultLauncher<String> requestPermission1 = registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
        @Override
        public void onActivityResult(Boolean result) {
            requestPermission2();
        }
    });


    private final ActivityResultLauncher<String[]> requestPermission2 = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
        @Override
        public void onActivityResult(Map<String, Boolean> result) {
//            initTongdunSDK();
            mainBinding.clMask.setVisibility(View.GONE);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected ViewBinding getViewBinding() {
        return mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initView() {
        super.initView();
        if (homeFragment == null) {
            homeFragment = HomeRunFragment.createFragment();
        }
        if (billRunFragment == null) {
            billRunFragment = BillRunFragment.createFragment();
        }
        if (BuildConfig.APPLICATION_ID.equals("com.wkbb.wukong")||BuildConfig.APPLICATION_ID.equals("com.wkbb.cc")) {
            loadRootFragment(R.id.main_fragment, homeFragment);
        } else if (BuildConfig.APPLICATION_ID.equals("com.fuyu.wk.bill")) {
            loadRootFragment(R.id.main_fragment, billRunFragment);
        }else {
            loadRootFragment(R.id.main_fragment, homeFragment);
        }


        mainBinding.clMask.setVisibility(View.GONE);
        checkAndRequestPermissions();
    }

    private void checkAndRequestPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (!AppConfig.getRequestPermission()) {
                AppConfig.setRequestPermission(true);
                requestPermission1();
            }
        }
    }

    // 申请设备信息权限
    private void requestPermission1() {
        mainBinding.clMask.setVisibility(View.VISIBLE);
        mainBinding.tvTipsTitle.setText("设备权限使用说明");
        String content = getString(R.string.app_name) + "正在向您获取“设备”权限，同意后，将用于以下用途：设备适配、保障服务顺畅，并验证用户身份保障账号安全。";
        mainBinding.tvTipsContent.setText(content);

        requestPermission1.launch(Manifest.permission.READ_PHONE_STATE);
    }

    // 申请存储权限
    private void requestPermission2() {
        mainBinding.tvTipsTitle.setText("存储权限使用说明");
        String content = getString(R.string.app_name) + "正在向您获取“存储”权限，同意后，将用于以下用途：实现账号、图片的缓存和使用，图片的保存与分享。";
        mainBinding.tvTipsContent.setText(content);

        requestPermission2.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE});
    }

    public void showHome() {
        pop();
    }

    public void showMe() {
        if (meFragment == null) {
            meFragment = MeFragment.createFragment();
        }
        start(meFragment);
    }
}